use chrono::Local;
use serde::{Deserialize, Serialize};
use sqlx::types::chrono::DateTime;
use struct_field_names_as_array::FieldNamesAsSlice;
use crate::core::serialize_tools::empty_string_as_none;

// 广告表单数据
#[derive(sqlx::FromRow, Deserialize, Serialize, FieldNamesAsSlice)]
pub struct AdvertForm { 
    // 位置id
    pub pid: i64,
    // 名称
    pub name: String, 
    // 广告连接地址
    pub url: String,
    // 广告图片
    pub image: String,
    // 排序
    pub order_by: i64
}


// 广告搜索
#[derive(Serialize, Deserialize)]
pub struct AdvertListReq {
    pub page: Option<i64>,
    pub page_size: Option<i64>,
    pub name: Option<String>,
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub pid: Option<i64>,
}

// 广告列表行数据
#[derive(sqlx::FromRow, Deserialize, Serialize)]
pub struct AdvertRow { 
    pub id: i64,
    pub advert_position_name: String,
    pub name: String, 
    pub url: String, 
    pub image: String, 
    pub hits: i64, 
    pub order_by: i64,
    pub created_at: DateTime<Local>,
    pub updated_at: DateTime<Local>
}

// 广告分页结果
#[derive(Serialize, Deserialize)]
pub struct AdvertListResp {
    pub paginate: String,
    pub list: Vec<AdvertRow>
}